package org.jeecg.modules.contract.pattern.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.business.entity.BussContractReviewDes;
import org.jeecg.modules.contract.pattern.entity.ErpGoodsPattern;
import org.jeecg.modules.contract.pattern.entity.ErpGoodsPatternDetail;
import org.jeecg.modules.contract.pattern.mapper.ErpGoodsPatternDetailMapper;
import org.jeecg.modules.contract.pattern.mapper.ErpGoodsPatternMapper;
import org.jeecg.modules.contract.pattern.service.IErpGoodsPatternService;
import org.jeecg.modules.contract.pattern.vo.ErpGoodsPatternPage;
import org.jeecg.modules.flowable.apithird.entity.SysUser;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 版样更改管理
 * @Author: jeecg-boot
 * @Date:   2024-05-27
 * @Version: V1.0
 */
@Service
public class ErpGoodsPatternServiceImpl extends ServiceImpl<ErpGoodsPatternMapper, ErpGoodsPattern> implements IErpGoodsPatternService {

	@Autowired
	private ErpGoodsPatternMapper erpGoodsPatternMapper;
	@Autowired
	private ErpGoodsPatternDetailMapper erpGoodsPatternDetailMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(ErpGoodsPattern erpGoodsPattern, List<ErpGoodsPatternDetail> erpGoodsPatternDetailList) {
		erpGoodsPatternMapper.insert(erpGoodsPattern);
		if(erpGoodsPatternDetailList!=null && erpGoodsPatternDetailList.size()>0) {
			for(ErpGoodsPatternDetail entity:erpGoodsPatternDetailList) {
				//外键设置
				entity.setPatternId(erpGoodsPattern.getId());
				erpGoodsPatternDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(ErpGoodsPattern erpGoodsPattern,List<ErpGoodsPatternDetail> erpGoodsPatternDetailList) {
		erpGoodsPatternMapper.updateById(erpGoodsPattern);
		
		//1.先删除子表数据
		erpGoodsPatternDetailMapper.deleteByMainId(erpGoodsPattern.getId());
		
		//2.子表数据重新插入
		if(erpGoodsPatternDetailList!=null && erpGoodsPatternDetailList.size()>0) {
			for(ErpGoodsPatternDetail entity:erpGoodsPatternDetailList) {
				//外键设置
				entity.setPatternId(erpGoodsPattern.getId());
				erpGoodsPatternDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		erpGoodsPatternDetailMapper.deleteByMainId(id);
		erpGoodsPatternMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			erpGoodsPatternDetailMapper.deleteByMainId(id.toString());
			erpGoodsPatternMapper.deleteById(id);
		}
	}

	@Override
	public IPage<ErpGoodsPatternPage> myPage(Page<ErpGoodsPattern> page,ErpGoodsPattern erpGoodsPattern) {
		return this.baseMapper.myPage(page, erpGoodsPattern);
	}

	@Override
	public ErpGoodsPatternDetail queryDetail(String id){
		return this.baseMapper.queryDetail(id);
	}

	@Override
	public void addPattern(ErpGoodsPatternDetail erpGoodsPatternDetail, HttpServletRequest req) {
		//根据包材修改版样停用状态
		this.baseMapper.updateStatus(erpGoodsPatternDetail.getPatternId());
		//新增新的版样信息
		erpGoodsPatternDetail.setId(String.valueOf(IdWorker.getId()));
		String username = JwtUtil.getUserNameByToken(req);
		erpGoodsPatternDetail.setCreateBy(username);
		erpGoodsPatternDetail.setCreateTime(new Date());
		this.baseMapper.addDetail(erpGoodsPatternDetail);
	}
}
